﻿using System;
using System.Collections.Generic;
using System.Linq;
using Bdcs.Application.Mappers.Dto;
using Bdcs.Application.Services.IServices;
using Bdcs.Core.Entity;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;

namespace Bdcs.Application.Services
{
    public class RouteTimeService : IRouteTimeService, ITransient
    {
        private readonly IRepository<DevRouteTimeMod> _routeTimeRepository;
        private readonly IRepository<SYS_CompanyInfoMod> _companyRepository;

        /// <summary>
        /// 构造方法注入
        /// </summary>
        /// <param name="routeTimeRepository"></param>
        /// <param name="companyRepository"></param>
        public RouteTimeService(IRepository<DevRouteTimeMod> routeTimeRepository,IRepository<SYS_CompanyInfoMod> companyRepository)
        {
            _routeTimeRepository = routeTimeRepository;
            _companyRepository = companyRepository;
        }


        /// <summary>
        /// 围栏时间段-分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public PagedList<RouteTimeDto> List(int pageIndex, int pageSize,string userId)
        {
            var query = @"Select RKey ,RegionID,RouteName ,StartTime, EndTime
            From DevRouteTime A INNER JOIN CRRouteInfo_M B 
            ON A.RegionID=B.RouteID
            Where B.CompanyID IN (Select CompanyID From Adv_UserDept where UserID=@userId)".SqlQuery<RouteTimeDto>(new
            {
                userId = userId
            });
            var pageList = new PagedList<RouteTimeDto>();
            int count = query.Count<RouteTimeDto>();
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            pageList.PageIndex = pageIndex;
            pageList.PageSize = pageSize;
            pageList.TotalCount = count;
            pageList.Items = list;
            if (count % pageSize == 0)
            {
                pageList.TotalPages = count / pageSize;
            }
            else
            {
                pageList.TotalPages = Convert.ToInt32(count / pageSize) + 1;
            }

            return pageList;
        }

        /// <summary>
        /// 围栏绑定设备
        /// </summary>
        /// <param name="rkey"></param>
        /// <param name="devList"></param>
        /// <returns></returns>
        public int SaveBindDev(string rkey, string devList)
        {
            var rowEffects =
                @"exec dbo.usp_Web_RegionTime_DeviceSave @Rkey,@devList "
                    .SqlNonQuery(new
                    {
                        Rkey = rkey,
                        devList = devList
                    });
            Console.WriteLine(rowEffects);
            return rowEffects;
        }

        /// <summary>
        /// 返回给定围栏对应的设备
        /// </summary>
        /// <param name="rkey"></param>
        /// <returns></returns>
        public string GetDevChecked(string rkey)
        {
            var value = @"exec dbo.usp_Web_RegionTime_DeviceChecked @rkey".SqlScalar(new {rkey = rkey});
            if (value == null)
            {
                return "";
            }
            return value.ToString();
        }

        /// <summary>
        /// 新增围栏时段
        /// </summary>
        /// <param name="routeTimeInput"></param>
        /// <returns></returns>
        public int Add(RouteTimeInput routeTimeInput)
        {
            var rowEffects =
                @"exec usp_Web_RegionTime_Save @Rkey,@RegionId,@StartTime,@EndTime,@OpUser,@Opmode"
                    .SqlNonQuery(new
                    {
                        Rkey = routeTimeInput.Id,
                        RegionId = routeTimeInput.RegionId,
                        StartTime = routeTimeInput.StartTime,
                        EndTime = routeTimeInput.EndTime,
                        OpUser = routeTimeInput.OpUser,
                        Opmode = routeTimeInput.Opmode
                    });
            Console.WriteLine(rowEffects);
            return rowEffects;
        }

        /// <summary>
        /// 删除围栏时段数据
        /// </summary>
        /// <param name="rkey"></param>
        /// <returns></returns>
        public DevRouteTimeMod delete(string rkey)
        {
            DevRouteTimeMod devRouteTimeMod = new DevRouteTimeMod();
            devRouteTimeMod.RKey = rkey;
            return _routeTimeRepository.Delete(devRouteTimeMod).Entity;
        }
    }
}